# 导包
import pymysql

# 创建连接
conn = pymysql.connect(host="211.103.136.244",
                       port=7061,
                       user="student",
                       password="iHRM_student_2021",
                       database="test_db",
                       charset="utf8")

# 获取游标
cursor = conn.cursor()

# 执行sql
# 查询t_book表中全局数据
sql = "select * from t_book;"
cursor.execute(sql)

# 获取第一行数据
print("游标的初始位置：", cursor.rownumber)
print(cursor.fetchone())
print("获取1行之后游标的位置：", cursor.rownumber)

# 获取查询结果集的第3条和第4条记录
cursor.rownumber = 2
print(cursor.fetchmany(2))

# 获取全部的查询结果集及总记录数
cursor.rownumber = 0
print("获取全部的查询结果集：", cursor.fetchall())
print("总记录行数：", cursor.rowcount)


# 关闭游标
cursor.close()

# 关闭连接
conn.close()
